package com.robert.recycleviewdemo

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View

class ItemDecoration(private val context: Context, @DrawableRes drawableRes: Int) : RecyclerView.ItemDecoration() {

    private val mDivider: Drawable?

    init {
        if (drawableRes != 0) {
            mDivider = ContextCompat.getDrawable(context, drawableRes)
        } else {
            mDivider = null
        }
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
//            drawVertical(canvas, parent)
//            drawHorizontal(canvas, parent)
        if (mDivider != null) {
            val childCount = parent.childCount
            for (i in 0 until childCount) {
                val child = parent.getChildAt(i)

                val params = child.layoutParams as RecyclerView.LayoutParams
                val top = child.top + params.topMargin - mDivider.intrinsicHeight
                val bottom = child.bottom - params.bottomMargin + mDivider.intrinsicHeight
                val left = child.left + params.leftMargin - mDivider.intrinsicWidth
                val right = child.right - params.rightMargin + mDivider.intrinsicWidth

                mDivider.setBounds(left, top, right, bottom)
                mDivider.draw(canvas)
            }
        }
    }

    private fun drawVertical(c: Canvas, parent: RecyclerView) {
        if (mDivider != null) {
            val childCount = parent.childCount
            for (i in 0 until childCount) {
                val child = parent.getChildAt(i)

                val params = child.layoutParams as RecyclerView.LayoutParams
                val top = child.top - params.topMargin
                val bottom = child.bottom + params.bottomMargin
                val left = child.right + params.rightMargin
                val right = left + mDivider.intrinsicWidth

                mDivider.setBounds(left, top, right, bottom)
                mDivider.draw(c)
            }
        }
    }


    private fun drawHorizontal(c: Canvas, parent: RecyclerView) {
        if (mDivider != null) {
            val childCount = parent.childCount
            for (i in 0 until childCount) {
                val child = parent.getChildAt(i)
                val params = child.layoutParams as RecyclerView.LayoutParams
                val left = child.left - params.leftMargin
                val right = (child.right + params.rightMargin + mDivider.getIntrinsicWidth())
                val top = child.bottom + params.bottomMargin
                val bottom = top + mDivider.intrinsicHeight
                mDivider.setBounds(left, top, right, bottom)
                mDivider.draw(c)
            }
        }
    }

    private fun getSpanCount(parent: RecyclerView): Int {
        // 列数
        var spanCount = -1
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            spanCount = layoutManager.spanCount
        } else if (layoutManager is StaggeredGridLayoutManager) {
            spanCount = layoutManager.spanCount
        } else if (layoutManager is LinearLayoutManager) {
            spanCount = 1
        }
        return spanCount
    }

    fun isLastColum(parent: RecyclerView, pos: Int, spanCount: Int, childCount: Int): Boolean {
        var childCount = childCount
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            if ((pos + 1) % spanCount == 0) {
                // 如果是最后一列，则不需要绘制右边
                return true
            }
        } else if (layoutManager is StaggeredGridLayoutManager) {
            val orientation = layoutManager.orientation
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0) {
                    // 如果是最后一列，则不需要绘制右边
                    return true
                }
            } else {
                childCount = childCount - childCount % spanCount
                if (pos >= childCount) {
                    // 如果是最后一列，则不需要绘制右边
                    return true
                }
            }
        }
        return false
    }

    private fun isLastRaw(parent: RecyclerView, pos: Int, spanCount: Int, childCount: Int): Boolean {
        var childCount = childCount
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            childCount = childCount - childCount % spanCount
            if (pos >= childCount) {
                // 如果是最后一行，则不需要绘制底部
                return true
            }
        } else if (layoutManager is StaggeredGridLayoutManager) {
            val orientation = layoutManager.orientation

            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                // StaggeredGridLayoutManager 且纵向滚动
                childCount = childCount - childCount % spanCount
                // 如果是最后一行，则不需要绘制底部
                if (pos >= childCount)
                    return true
            } else {
                // StaggeredGridLayoutManager 且横向滚动
                // 如果是最后一行，则不需要绘制底部
                if ((pos + 1) % spanCount == 0) {
                    return true
                }
            }
        }
        return false
    }


    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val itemPosition = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
        val spanCount = getSpanCount(parent)
        val childCount = parent.adapter.itemCount
        val divider = DensityUtils.dp2px(context, 6f);
        when {
        // 如果是最后一行，绘制底部
            isLastRaw(parent, itemPosition, spanCount, childCount) -> outRect.set(divider, divider, 0, divider)
        // 如果是最后一列，绘制右边
            isLastColum(parent, itemPosition, spanCount, childCount) -> outRect.set(divider, divider, divider, 0)
        //不是最后一行或者最后一列，只绘制左上
            else -> outRect.set(divider, divider, 0, 0)
        }

    }


}